package siena.dbslayer;

import java.util.Collection;
import java.util.List;

import siena.Model;
import siena.Util;

/**
 * This class contains the logic to create the basic statement for SQL.
 * This class not execute or prepared the statement, only create the plain text
 * @author jsanca
 *
 */
public class GenericSQLPlainStatementBuilder {

	
   /**
    * Create the select without "where or delimit tokens"
    * @param model Model.
    * @return String
    */
   public String createSelectAll (Model model) {
	   
	   StringBuilder builder = new StringBuilder ();
	   Collection<String> columnNames = null;
	   String  tableName = ModelHelper.getTableName(model.getClass());
	   
	   columnNames = ModelHelper.getColumnNames(model);
	   
	   builder.append("SELECT ");
	   
	   builder.append(Util.join(columnNames, ", "));
	   
	   builder.append(" FROM ").append(tableName);
	   
	   return builder.toString();
   } // createSelect.
   
   /**
    * Create a select statement based in the keyColumns to build the where statement, 
    * optionally you can send a baseSelectSQL, if baseSelectSQL is null, will create the a new baseSelectSQL calling the createSelectAll method.
    * @param model Model.
    * @param keyColumns List<Strin>
    * @param baseSelectSQL String
    * @return String
    */
   public String createSelect (Model model, List<String> keyColumns, String baseSelectSQL) {
	   
	   StringBuilder selectSQL = new StringBuilder ( (null != baseSelectSQL)?baseSelectSQL:this.createSelectAll(model));
	   selectSQL.append(" WHERE ");
	   selectSQL.append(Util.join(keyColumns, " AND "));
	   return selectSQL.toString();
   } // createSelect.
   
   /**
    * Create a insert statement based in the inserColumns to set the values of the class.
    * @param model Model
    * @param insertColumns List<String>
    * @return String
    */
   public String createInsert (Model model, List<String> insertColumns) {
	   
	   StringBuilder insertSQL = new StringBuilder ("INSERT INTO ");
	   String tableName = ModelHelper.getTableName(model.getClass());
	   insertSQL.append(tableName).append (" SET ").append (Util.join(insertColumns, ", "));
	   
	   return insertSQL.toString();
   } // createInsert.
   
   /**
    * Create a delete statement based in the keyColumns to build the where sentence.
    * @param model Model
    * @param keyColumns List<String>
    * @return String
    */
   public String createDelete (Object model, List<String> keyColumns) {
	   
	   String tableName = ModelHelper.getTableName(model.getClass());
	   StringBuilder deleteSQL = new StringBuilder ("DELETE FROM ");
	   deleteSQL.append(tableName).append(" WHERE ").append(Util.join(keyColumns, " AND "));
	   return deleteSQL.toString();
   } // createDelete.
   
   /**
    * Create a update statement based in the updateColumns (in the set sentence) and keyColumns to build the where sentence.
    * @param model Model
    * @param updateColumns List<String>
    * @param keyColumns List<String>
    * @return String
    */
   public String createUpdate (Model model, List<String> updateColumns, List<String> keyColumns) {
	   
	   String tableName = ModelHelper.getTableName(model.getClass());
	   StringBuilder updateSQL = new StringBuilder ("UPDATE ");
	   updateSQL.append (tableName).append(" SET ");
	   updateSQL.append (Util.join(updateColumns, ", "));
	   updateSQL.append (" WHERE ");
	   updateSQL.append (Util.join(keyColumns, " AND "));

	   return updateSQL.toString();
   } // createUpdate.
} // E:O:F:AbtractSQLQueryBuilder.
